package cn.hhchat.wp.service;

import cn.hhchat.wp.dto.form.PostForm;
import cn.hhchat.wp.entity.PostEntity;
import cn.hhchat.wp.entity.GroupEntity;
import cn.hhchat.wp.entity.UserEntity;
import cn.hhchat.wp.repository.PostRepository;
import cn.hhchat.wp.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created this one by HMH on 2017/10/23.
 */
@Slf4j
@Service
@Transactional
public class PostService {


    public int countPostThisWeekByGroup(GroupEntity group) {
        LocalDate now = LocalDate.now();
        return postRepository.countByWeekOfYearAndYearAndGroupOrderByCreateTimeDesc(DateUtil.getWeekOfYear(now), now.getYear(), group);
    }

    public long countPosterThisWeekByGroup(GroupEntity group) {
        Instant start = Instant.now();
        log.info("check date bound "+ Duration.between(start, Instant.now()).toMillis());
        int week = DateUtil.getWeekOfYear(LocalDate.now());
        List<PostEntity> posts = postRepository.findByWeekOfYearAndYearAndGroupOrderByCreateTimeDesc(week,LocalDate.now().getYear(), group);
        log.info("check search "+ Duration.between(start, Instant.now()).toMillis());
        return posts.stream().map(PostEntity::getPoster).distinct().count();
    }

    public PostEntity createOne(PostForm postForm, UserEntity user, GroupEntity group) {
        PostEntity post = new PostEntity();
        post.setPoster(user);
        LocalDate localDate = LocalDate.now();
        post.setYear(localDate.getYear());
        post.setWeekOfYear(DateUtil.getWeekOfYear(localDate));
        post.setGroup(group);
        post.setMarkContent(postForm.getMarkContent());
        post.setContent(postForm.getContent());
        post.setAttachments(postForm.getAttachments().stream().map(a -> attachmentService.getById(a)).collect(Collectors.toList()));

        return save(post);
    }

    public PostEntity save(PostEntity post) {
        return postRepository.save(post);
    }

    public List<PostEntity> getPostOfUserInWeek(GroupEntity group, UserEntity user, int year, int week) {
        return postRepository.findByWeekOfYearAndYearAndGroupAndPosterOrderByCreateTimeDesc(week, year, group, user);
    }

    public List<PostEntity> getPostOfGroupInWeek(GroupEntity group, int year, int week) {
        return postRepository.findByWeekOfYearAndYearAndGroupOrderByCreateTimeDesc(week, year, group);
    }

    public List<PostEntity> getLastFivePostOfUser(GroupEntity group, UserEntity user){
        return postRepository.findTop5ByGroupAndPosterOrderByCreateTimeDesc(group, user);
    }

    public PostEntity getById(long id) {
        return postRepository.findOne(id);
    }

    public PostEntity edit(PostEntity post, PostForm postForm) {
        post.setContent(postForm.getContent());
        post.setMarkContent(postForm.getMarkContent());
        post.setAttachments(postForm.getAttachments().stream().map(a -> attachmentService.getById(a)).collect(Collectors.toList()));

        return save(post);
    }

    @Autowired
    private PostRepository postRepository;


    @Autowired
    private AttachmentService attachmentService;
}
